

#=======================【VM机器,二进制安装】

# 参考网址：

# https://github.com/ctripcorp/apollo/issues/3177

#========================install apollo========================================================================                                              



# =============================================================================================================

# step 1: 创建数据库
# 配置Apollo需要的portaldb和configdb
 
# 1. 创建ApolloPortalDB，如下
# 打开附件SQL: soft\apollo\files\mysql\apolloportaldb.sql
# 然后，复制SQL在MySQL客户端工具（navicat mysql）执行。
# 执行创建DB完毕后，执行下面查询SQL，检查结果是否OK
# mysql> use apolloportaldb;
# Database changed
# mysql> 
# mysql> select `Id`, `Key`, `Value`, `Comment` from `ApolloPortalDB`.`ServerConfig` limit 1;
# +----+--------------------+-------+------------------+
# | Id | Key                | Value | Comment          |
# +----+--------------------+-------+------------------+
# |  1 | apollo.portal.envs | dev   | 可支持的环境列表 |
# +----+--------------------+-------+------------------+
# 1 row in set
# mysql> 


# ----------------------------------------------------------

# 2. 创建
# 需要在每个环境部署一套，如fat、uat和pro分别部署3套ApolloConfigDB。
# 打开附件SQL: soft\apollo\files\mysql\apolloconfigdb.sql
# 然后，复制SQL在MySQL客户端工具（navicat mysql）执行。
# 修改SQL脚本，分别如下
# 开发环境dev ，修改数据库名称为 ApolloConfigDB_dev
# 测试环境sit ，修改数据库名称为 ApolloConfigDB_sit
# 测试环境uat ，修改数据库名称为 ApolloConfigDB_uat
# 生产环境prod ，修改数据库名称为 ApolloConfigDB_prod
# 执行创建DB完毕后，执行下面查询SQL，检查结果是否OK
# mysql> use apolloconfigdb_uat;
# Database changed
# mysql> 
# mysql> select `Id`, `Key`, `Value`, `Comment` from `ApolloConfigDB_uat`.`ServerConfig` limit 1;
# +----+--------------------+-------------------------------+------------------------------------------+
# | Id | Key                | Value                         | Comment                                  |
# +----+--------------------+-------------------------------+------------------------------------------+
# |  1 | eureka.service.url | http://localhost:8080/eureka/ | Eureka服务Url，多个service以英文逗号分隔 |
# +----+--------------------+-------------------------------+------------------------------------------+
# 1 row in set
# mysql> 


# --------------------------------------------------------------------------------------------------------------------

# 创建mysql 数据库账号 app_apollo
# 创建用户(user1:用户名;%:任意ip,也可以指定
mysql root >> create user 'app_apollo'@'%' identified by 'password';
# 设置用户，可以在其他客户端软件链接mysql数据库
mysql root >> alter user 'app_apollo'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
# 授权，默认创建的用户权限是usage,就是无权限，只能登录而已。
# all：所有权限，这里有select,update等等权限，可以去搜一下；后面的*.*：指定数据库.指定表，这里是所有；to后面就是你刚才创建的用户
mysql root >> grant all on apolloportaldb.* to 'app_apollo'@'%';
mysql root >> grant all on apolloconfigdb_uat.* to 'app_apollo'@'%';
mysql root >> grant all on apolloconfigdb_sit.* to 'app_apollo'@'%';
mysql root >> grant all on apolloconfigdb_dev.* to 'app_apollo'@'%';
mysql root >> grant all on apolloconfigdb_prod.* to 'app_apollo'@'%';


# --------------------------------------------------------------------------------------------------------------------

# 3. 从别的环境导入ApolloConfigDB的项目数据
# https://github.com/ctripcorp/apollo/wiki/分布式部署指南
# 参考章节：2.1.2.1 从别的环境导入ApolloConfigDB的项目数据


# --------------------------------------------------------------------------------------------------------------------

# 4. 调整服务端配置
# https://github.com/ctripcorp/apollo/wiki/分布式部署指南
# 参考章节：2.1.3 调整服务端配置
# 2.1.3.1 调整ApolloPortalDB配置
# 2.1.3.2 调整ApolloConfigDB配置

# 4.1. portaldb 修改配置serverconfig表的全局参数

# 修改SQL, 1. apollo.portal.envs - 可支持的环境列表, 
# 默认值是dev，如果portal需要管理多个环境的话，以逗号分隔即可（大小写不敏感）
mysql root >> use apolloportaldb; # portaldb
mysql root >> update serverconfig set value = 'DEV,SIT,UAT' where `key` = 'apollo.portal.envs' ;
# 输出如下：
# Query OK, 1 row affected
# Rows matched: 1  Changed: 1  Warnings: 0



# 修改SQL, 2. apollo.portal.meta.servers - 各环境Meta Service列表
# Apollo Portal需要在不同的环境访问不同的meta service(apollo-configservice)地址，所以我们需要在配置中提供这些信息。
# 默认情况下，meta service和config service是部署在同一个JVM进程，所以meta service的地址就是config service的地址。
mysql root >> use apolloportaldb;  # portaldb
mysql root >> update serverconfig set value = '{
    "DEV":"http://CNT7XAPLOD01:18081",
    "SIT":"http://CNT7XAPLOD01:18082",
    "UAT":"http://CNT7XAPLOD01:18083"
}' 
where `key` = 'apollo.portal.meta.servers';

# 对应apollo-portal服务下面的config/apollo-env.properties 内容，如下：
# local.meta=http://CNT7XAPLOD01:18081
# dev.meta=http://CNT7XAPLOD01:18081
# sit.meta=http://CNT7XAPLOD01:18082
# uat.meta=http://CNT7XAPLOD01:18083
# prod.meta=http://CNT7XAPLOD01:18084



# 修改SQL, 3. configView.memberOnly.envs - 设置网页昨天导航菜单上面展示的ENV环境清单
mysql root >> use apolloportaldb;  # portaldb
mysql root >> update serverconfig set value = 'DEV,SIT,UAT' where `key` = 'configView.memberOnly.envs' ;


# -----------------------------------------------------------------------------------


# 4.2. configdb_dev/sit/uat/prod等，  修改配置serverconfig表的全局参数

# 修改SQL, 1. eureka.service.url - Eureka服务Url
# 节点1: configdb dev ，eureka对应地址为config-server的地址端口 + "/eureka"  , 就是 http://CNT7XAPLOD01:18081/eureka
mysql root >> use apolloconfigdb_dev; # configdb 
mysql root >> update serverconfig set `value` = 'http://CNT7XAPLOD01:18081/eureka'
where `key` = 'eureka.service.url'

# 节点2: configdb sit ，eureka对应地址为config-server的地址端口 + "/eureka"  , 就是 http://CNT7XAPLOD01:18082/eureka
mysql root >> use apolloconfigdb_sit; # configdb 
mysql root >> update serverconfig set `value` = 'http://CNT7XAPLOD01:18082/eureka'
where `key` = 'eureka.service.url'

# 节点3: configdb uat ，eureka对应地址为config-server的地址端口 + "/eureka"  , 就是 http://CNT7XAPLOD01:1803/eureka
mysql root >> use apolloconfigdb_uat; # configdb 
mysql root >> update serverconfig set `value` = 'http://CNT7XAPLOD01:18083/eureka'
where `key` = 'eureka.service.url'


# --------------------------------------------------------------------------------------------------------------------

# 5. 配置Apollo需要的应用程序和配置文件

# https://github.com/ctripcorp/apollo/wiki/分布式部署指南
# 参考章节：2.2.1.1 直接下载安装包

# 参考章节：2.2.1.1.1 获取apollo-configservice、apollo-adminservice、apollo-portal安装包

# 参考章节：2.2.1.1.2 配置数据库连接信息

# 参考章节：2.2.1.1.2.2 配置apollo-adminservice的数据库连接信息

# 参考章节：2.2.1.1.2.3 配置apollo-portal的数据库连接信息

# 参考章节：2.2.1.1.2.4 配置apollo-portal的meta service信息


# =============================================================================================================

# step 2: 创建安装目录
mkdir -p /opt/soft/{jdk,apollo}
# 然后上传jdk或apollo 二进制文件到上面创建的软件目录

# 创建安装主目录
mkdir -p /app/apollo/{apollo-configservice-1,apollo-adminservice-1,apollo-portal-1} && \
# 创建数据主目录
mkdir -p /data/apollo/{apollo-configservice-1,apollo-adminservice-1,apollo-portal-1} && \
# 创建日志主目录
mkdir -p /log/apollo/{apollo-configservice-1,apollo-adminservice-1,apollo-portal-1}


# =============================================================================================================

# step 3: 安装用户和组的创建
# 使用root用户，进行如下操作：
# 创建ops_install组
groupadd ops_install_group
# groupdel ops_install_group # 删除群组

# 创建ops_admin_group组
groupadd ops_admin_group

# 创建ops_app_user用户
useradd -g ops_install_group  -G  ops_admin_group  ops_app_user

# 修改ops_app_user密码
echo 'password'|passwd --stdin ops_app_user

# 删除用户和其以来的用户文件
# userdel -r ops_app_user
# 查看用户ops_app_user权限是否设置正确，正确输出结果如下
# id ops_app_user
# [root@CNT7XAPLOD01 ~]# id ops_app_user
# uid=1002(ops_app_user) gid=5003(ops_install_group) groups=5003(ops_install_group),5004(ops_admin_group)

# =============================================================================================================

# step 4: 安装用户的profile文件的设置

# 编辑/etc/profile，加入以下内容
vi /etc/profile
# -----------------------java env-----------------------------------------------------------------
JAVA_HOME=/env/jdk/jdk-12.0.2
PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
# -----------------------java env-----------------------------------------------------------------


# 生效配置环境变量
source /etc/profile

# 检查生效环境变量
env | grep JAVA

# =============================================================================================================

# step 5: 安装jdk 8 
# 创建jdk的软件目录和安装目录，分别如下：
mkdir -p /opt/soft/jdk/ && \
mkdir -p /env/jdk/

# 然后，通过WinSCP工具，把JDK 8二进制安装包tar复制到软件目录
ls -al /opt/soft/jdk/jdk-12.0.2_linux-x64_bin.tar.gz

# 解压jdk到安装目录
tar -zxvf /opt/soft/jdk/jdk-12.0.2_linux-x64_bin.tar.gz -C /env/jdk/


# 解压apollo安装文件
unzip -o /opt/soft/apollo/apollo-adminservice-1.6.1-github.zip -d /opt/soft/apollo/apollo-adminservice-1.6.1/
unzip -o /opt/soft/apollo/apollo-configservice-1.6.1-github.zip -d /opt/soft/apollo/apollo-configservice-1.6.1/
unzip -o /opt/soft/apollo/apollo-portal-1.6.1-github.zip -d /opt/soft/apollo/apollo-portal-1.6.1/


# --------------------------------------------------------------------------------------------------------------------

# 配置Apollo需要的应用程序和配置文件

# https://github.com/ctripcorp/apollo/wiki/分布式部署指南
# 参考章节：2.2.1.1 直接下载安装包
# 参考章节：2.2.1.1.1 获取apollo-configservice、apollo-adminservice、apollo-portal安装包
# 参考章节：2.2.1.1.2 配置数据库连接信息


# 配置apollo-configservice的数据库连接信息, 这里是DEV环境
vi /opt/soft/apollo/apollo-configservice-1.6.1/config/application-github.properties
spring.datasource.url=jdbc:mysql://CNT7XAPLOD01:13306/ApolloConfigDB_DEV?characterEncoding=utf8
spring.datasource.username=app_apollo
spring.datasource.password=password


# 配置apollo-adminservice的数据库连接信息, 这里是DEV环境
vi /opt/soft/apollo/apollo-adminservice-1.6.1/config/application-github.properties
spring.datasource.url=jdbc:mysql://CNT7XAPLOD01:13306/ApolloConfigDB_DEV?characterEncoding=utf8
spring.datasource.username=app_apollo
spring.datasource.password=password


# 配置apollo-portal的数据库连接信息
vi /opt/soft/apollo/apollo-portal-1.6.1/config/application-github.properties
# DataSource
spring.datasource.url=jdbc:mysql://CNT7XAPLOD01:13306/ApolloPortalDB?characterEncoding=utf8
spring.datasource.username=app_apollo
spring.datasource.password=password


# 配置apollo-portal的meta service信息
vi /opt/soft/apollo/apollo-portal-1.6.1/config/apollo-env.properties
# local.meta=http://localhost:8080
# dev.meta=http://fill-in-dev-meta-server:8080
# fat.meta=http://fill-in-fat-meta-server:8080
# uat.meta=http://fill-in-uat-meta-server:8080
# lpt.meta=${lpt_meta}
# pro.meta=http://fill-in-pro-meta-server:8080
# 改成如下： dev / sit / uat / prod
# 注意meta service的端口对应configservice组件的文件scripts/startup.sh里面的参数SERVER_PORT
local.meta=http://CNT7XAPLOD01:18081
dev.meta=http://CNT7XAPLOD01:18081
sit.meta=http://CNT7XAPLOD01:18082
uat.meta=http://CNT7XAPLOD01:18083
prod.meta=http://CNT7XAPLOD01:18084


# 其他组建的端口介绍，如下
# 服务启动顺寻 config --> adminservice --> portal
portal == SERVER_PORT=${SERVER_PORT:=8070}
configservice == SERVER_PORT=${SERVER_PORT:=8080}
adminservice == SERVER_PORT=${SERVER_PORT:=8090}
 

 
# 配置apollo-configservice的JVM参数
vi /opt/soft/apollo/apollo-configservice-1.6.1/scripts/startup.sh
# 修改JVM参数如下, 内存参数根据硬件实际情况设置
# export JAVA_OPTS="-Xms6144m -Xmx6144m -Xss256k -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=384m -XX:NewSize=4096m -XX:MaxNewSize=4096m -XX:SurvivorRatio=8"
export JAVA_OPTS="-Xms128m -Xmx256m -Xss256k -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:SurvivorRatio=8"


# 配置apollo-adminservice的JVM参数
vi /opt/soft/apollo/apollo-adminservice-1.6.1/scripts/startup.sh
# 修改JVM参数如下, 内存参数根据硬件实际情况设置
# export JAVA_OPTS="-Xms2560m -Xmx2560m -Xss256k -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=384m -XX:NewSize=1536m -XX:MaxNewSize=1536m -XX:SurvivorRatio=8"
export JAVA_OPTS="-Xms128m -Xmx256m -Xss256k -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:SurvivorRatio=8"


# 配置apollo-portal的JVM参数
vi /opt/soft/apollo/apollo-portal-1.6.1/scripts/startup.sh
# 修改JVM参数如下, 内存参数根据硬件实际情况设置
# export JAVA_OPTS="-Xms2560m -Xmx2560m -Xss256k -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=384m -XX:NewSize=1536m -XX:MaxNewSize=1536m -XX:SurvivorRatio=8"
export JAVA_OPTS="-Xms128m -Xmx256m -Xss256k -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:SurvivorRatio=8"

# =============================================================================================================

# 配置 伪集群, 节点1/2/3
# step 6: 创建相应的文件系统（或安装目录）
# for 循环 - begin
V_NODE_NUM=3
for ((i=1;i<=${V_NODE_NUM};i++))
do

# apollo-portal
APOLLO_PORTAL_HOME_i=/app/apollo/apollo-portal-${i}
APOLLO_PORTAL_DATA_i=/data/apollo/apollo-portal-${i}
APOLLO_PORTAL_LOG_i=/log/apollo/apollo-portal-${i}
# apollo-adminservice
APOLLO_ADMINSERVICE_HOME_i=/app/apollo/apollo-adminservice-${i}
APOLLO_ADMINSERVICE_DATA_i=/data/apollo/apollo-adminservice-${i}
APOLLO_ADMINSERVICE_LOG_i=/log/apollo/apollo-adminservice-${i}
# apollo-configservice
APOLLO_CONFIGSERVICE_HOME_i=/app/apollo/apollo-configservice-${i}
APOLLO_CONFIGSERVICE_DATA_i=/data/apollo/apollo-configservice-${i}
APOLLO_CONFIGSERVICE_LOG_i=/log/apollo/apollo-configservice-${i}


# 创建文件目录
mkdir -p ${APOLLO_PORTAL_HOME_i} && \
mkdir -p ${APOLLO_PORTAL_DATA_i} && \
mkdir -p ${APOLLO_PORTAL_LOG_i} && \
#
mkdir -p ${APOLLO_ADMINSERVICE_HOME_i} && \
mkdir -p ${APOLLO_ADMINSERVICE_DATA_i} && \
mkdir -p ${APOLLO_ADMINSERVICE_LOG_i} && \
#
mkdir -p ${APOLLO_CONFIGSERVICE_HOME_i} && \
mkdir -p ${APOLLO_CONFIGSERVICE_DATA_i} && \
mkdir -p ${APOLLO_CONFIGSERVICE_LOG_i}


# 复制安装文件
cp -rf /opt/soft/apollo/apollo-portal-1.6.1/ -d ${APOLLO_PORTAL_HOME_i}/apollo-portal-1.6.1/
cp -rf /opt/soft/apollo/apollo-adminservice-1.6.1/ -d ${APOLLO_ADMINSERVICE_HOME_i}/apollo-adminservice-1.6.1/
cp -rf /opt/soft/apollo/apollo-configservice-1.6.1/ -d ${APOLLO_CONFIGSERVICE_HOME_i}/apollo-configservice-1.6.1/

done
# for 循环 - end

ls -al /app/apollo
# 输出结果，如下图 

ls -al /data/apollo
# 输出结果，如下图 

ls -al /log/apollo
# 输出结果，如下图 


# =============================================================================================================

# step 7: 配置 伪集群, 节点1/2/3

# step 7.1: 配置节点1: DEV环境

# --------------------------------------------------

# 1. 配置 configservice

# 
# 修改配置文件 configservice (configservice 和 eureka 是同一个JVM)
vi /app/apollo/apollo-configservice-1/apollo-configservice-1.6.1/apollo-configservice.conf
# 修改配置文件,如下：
# LOG_FOLDER=/opt/logs/100003171/
LOG_FOLDER=/log/apollo/apollo-configservice-1/apollo-configservice-1.6.1/

# 修改配置文件 
vi /app/apollo/apollo-configservice-1/apollo-configservice-1.6.1/scripts/startup.sh
# 修改配置文件,如下：
# LOG_DIR=/opt/logs/100003171
# SERVER_PORT=${SERVER_PORT:=8080}
LOG_DIR=/log/apollo/apollo-configservice-1/apollo-configservice-1.6.1/
SERVER_PORT=${SERVER_PORT:=18081}

# 修改配置文件: config dev 数据库
vi /app/apollo/apollo-configservice-1/apollo-configservice-1.6.1/config/application-github.properties
spring.datasource.url=jdbc:mysql://CNT7XAPLOD01:13306/ApolloConfigDB_DEV?characterEncoding=utf8
spring.datasource.username=app_apollo
spring.datasource.password=password#123


# --------------------------------------------------

# 2. 配置 adminservice

# 修改配置文件
vi /app/apollo/apollo-adminservice-1/apollo-adminservice-1.6.1/apollo-adminservice.conf
# 修改配置文件,如下：
# LOG_FOLDER=/opt/logs/100003172/
LOG_FOLDER=/log/apollo/apollo-adminservice-1/apollo-adminservice-1.6.1/

# 修改配置文件 
vi /app/apollo/apollo-adminservice-1/apollo-adminservice-1.6.1/scripts/startup.sh
# 修改配置文件,如下：
# LOG_DIR=/opt/logs/100003172
# SERVER_PORT=${SERVER_PORT:=8090}
LOG_DIR=/log/apollo/apollo-adminservice-1/apollo-adminservice-1.6.1/
SERVER_PORT=${SERVER_PORT:=18091}

# 修改配置文件: config dev 数据库
vi /app/apollo/apollo-adminservice-1/apollo-adminservice-1.6.1/config/application-github.properties
spring.datasource.url=jdbc:mysql://CNT7XAPLOD01:13306/ApolloConfigDB_DEV?characterEncoding=utf8
spring.datasource.username=app_apollo
spring.datasource.password=password#123

# --------------------------------------------------

# 3. 配置 portal

# 修改配置文件
vi /app/apollo/apollo-portal-1/apollo-portal-1.6.1/apollo-portal.conf
# 修改配置文件,如下：
# LOG_FOLDER=/opt/logs/100003173/
LOG_FOLDER=/log/apollo/apollo-portal-1/apollo-portal-1.6.1/

# 修改配置文件 
vi /app/apollo/apollo-portal-1/apollo-portal-1.6.1/scripts/startup.sh
# 修改配置文件,如下：
# SERVICE_NAME=apollo-portal
# LOG_DIR=/opt/logs/100003173
# SERVER_PORT=${SERVER_PORT:=8070}
LOG_DIR=/log/apollo/apollo-portal-1/apollo-portal-1.6.1/
SERVER_PORT=${SERVER_PORT:=18071}

# 修改配置文件
vi /app/apollo/apollo-portal-1/apollo-portal-1.6.1/config/application-github.properties
spring.datasource.url=jdbc:mysql://CNT7XAPLOD01:13306/ApolloPortalDB?characterEncoding=utf8
spring.datasource.username=app_apollo
spring.datasource.password=password#123


# ======================================================

# step 7.2: 配置节点2: SIT环境

# --------------------------------------------------

# 1. 配置 configservice

# 
# 修改配置文件 configservice (configservice 和 eureka 是同一个JVM)
vi /app/apollo/apollo-configservice-2/apollo-configservice-1.6.1/apollo-configservice.conf
# 修改配置文件,如下：
# LOG_FOLDER=/opt/logs/100003171/
LOG_FOLDER=/log/apollo/apollo-configservice-2/apollo-configservice-1.6.1/

# 修改配置文件 
vi /app/apollo/apollo-configservice-2/apollo-configservice-1.6.1/scripts/startup.sh
# 修改配置文件,如下：
# LOG_DIR=/opt/logs/100003171
# SERVER_PORT=${SERVER_PORT:=8080}
LOG_DIR=/log/apollo/apollo-configservice-2/apollo-configservice-1.6.1/
SERVER_PORT=${SERVER_PORT:=18082}

# 修改配置文件: config sit 数据库
vi /app/apollo/apollo-configservice-2/apollo-configservice-1.6.1/config/application-github.properties
spring.datasource.url=jdbc:mysql://CNT7XAPLOD01:13306/ApolloConfigDB_SIT?characterEncoding=utf8
spring.datasource.username=app_apollo
spring.datasource.password=password#123


# --------------------------------------------------

# 2. 配置 adminservice

# 修改配置文件
vi /app/apollo/apollo-adminservice-2/apollo-adminservice-1.6.1/apollo-adminservice.conf
# 修改配置文件,如下：
# LOG_FOLDER=/opt/logs/100003172/
LOG_FOLDER=/log/apollo/apollo-adminservice-2/apollo-adminservice-1.6.1/

# 修改配置文件 
vi /app/apollo/apollo-adminservice-2/apollo-adminservice-1.6.1/scripts/startup.sh
# 修改配置文件,如下：
# LOG_DIR=/opt/logs/100003172
# SERVER_PORT=${SERVER_PORT:=8090}
LOG_DIR=/log/apollo/apollo-adminservice-2/apollo-adminservice-1.6.1/
SERVER_PORT=${SERVER_PORT:=18092}

# 修改配置文件: config sit 数据库
vi /app/apollo/apollo-adminservice-2/apollo-adminservice-1.6.1/config/application-github.properties
spring.datasource.url=jdbc:mysql://CNT7XAPLOD01:13306/ApolloConfigDB_SIT?characterEncoding=utf8
spring.datasource.username=app_apollo
spring.datasource.password=password#123

# --------------------------------------------------

# 3. 配置 portal

# 修改配置文件
vi /app/apollo/apollo-portal-2/apollo-portal-1.6.1/apollo-portal.conf
# 修改配置文件,如下：
# LOG_FOLDER=/opt/logs/100003173/
LOG_FOLDER=/log/apollo/apollo-portal-2/apollo-portal-1.6.1/

# 修改配置文件 
vi /app/apollo/apollo-portal-2/apollo-portal-1.6.1/scripts/startup.sh
# 修改配置文件,如下：
# SERVICE_NAME=apollo-portal
# LOG_DIR=/opt/logs/100003173
# SERVER_PORT=${SERVER_PORT:=8070}
LOG_DIR=/log/apollo/apollo-portal-2/apollo-portal-1.6.1/
SERVER_PORT=${SERVER_PORT:=18072}

# 修改配置文件
vi /app/apollo/apollo-portal-2/apollo-portal-1.6.1/config/application-github.properties
spring.datasource.url=jdbc:mysql://CNT7XAPLOD01:13306/ApolloPortalDB?characterEncoding=utf8
spring.datasource.username=app_apollo
spring.datasource.password=password#123

# ======================================================

# step 7.3: 配置节点3: UAT环境

# --------------------------------------------------

# 1. 配置 configservice

# 
# 修改配置文件 configservice (configservice 和 eureka 是同一个JVM)
vi /app/apollo/apollo-configservice-3/apollo-configservice-1.6.1/apollo-configservice.conf
# 修改配置文件,如下：
# LOG_FOLDER=/opt/logs/100003171/
LOG_FOLDER=/log/apollo/apollo-configservice-3/apollo-configservice-1.6.1/

# 修改配置文件 
vi /app/apollo/apollo-configservice-3/apollo-configservice-1.6.1/scripts/startup.sh
# 修改配置文件,如下：
# LOG_DIR=/opt/logs/100003171
# SERVER_PORT=${SERVER_PORT:=8080}
LOG_DIR=/log/apollo/apollo-configservice-3/apollo-configservice-1.6.1/
SERVER_PORT=${SERVER_PORT:=18083}

# 修改配置文件: config uat 数据库
vi /app/apollo/apollo-configservice-3/apollo-configservice-1.6.1/config/application-github.properties
spring.datasource.url=jdbc:mysql://CNT7XAPLOD01:13306/ApolloConfigDB_UAT?characterEncoding=utf8
spring.datasource.username=app_apollo
spring.datasource.password=password#123


# --------------------------------------------------

# 2. 配置 adminservice

# 修改配置文件
vi /app/apollo/apollo-adminservice-3/apollo-adminservice-1.6.1/apollo-adminservice.conf
# 修改配置文件,如下：
# LOG_FOLDER=/opt/logs/100003172/
LOG_FOLDER=/log/apollo/apollo-adminservice-3/apollo-adminservice-1.6.1/

# 修改配置文件 
vi /app/apollo/apollo-adminservice-3/apollo-adminservice-1.6.1/scripts/startup.sh
# 修改配置文件,如下：
# LOG_DIR=/opt/logs/100003172
# SERVER_PORT=${SERVER_PORT:=8090}
LOG_DIR=/log/apollo/apollo-adminservice-3/apollo-adminservice-1.6.1/
SERVER_PORT=${SERVER_PORT:=18093}

# 修改配置文件: config uat 数据库
vi /app/apollo/apollo-adminservice-3/apollo-adminservice-1.6.1/config/application-github.properties
spring.datasource.url=jdbc:mysql://CNT7XAPLOD01:13306/ApolloConfigDB_UAT?characterEncoding=utf8
spring.datasource.username=app_apollo
spring.datasource.password=password#123

# --------------------------------------------------

# 3. 配置 portal

# 修改配置文件
vi /app/apollo/apollo-portal-3/apollo-portal-1.6.1/apollo-portal.conf
# 修改配置文件,如下：
# LOG_FOLDER=/opt/logs/100003173/
LOG_FOLDER=/log/apollo/apollo-portal-3/apollo-portal-1.6.1/

# 修改配置文件 
vi /app/apollo/apollo-portal-3/apollo-portal-1.6.1/scripts/startup.sh
# 修改配置文件,如下：
# SERVICE_NAME=apollo-portal
# LOG_DIR=/opt/logs/100003173
# SERVER_PORT=${SERVER_PORT:=8070}
LOG_DIR=/log/apollo/apollo-portal-3/apollo-portal-1.6.1/
SERVER_PORT=${SERVER_PORT:=18073}

# 修改配置文件
vi /app/apollo/apollo-portal-3/apollo-portal-1.6.1/config/application-github.properties
spring.datasource.url=jdbc:mysql://CNT7XAPLOD01:13306/ApolloPortalDB?characterEncoding=utf8
spring.datasource.username=app_apollo
spring.datasource.password=password#123

# ======================================================

# =============================================================================================================

# 配置伪集群
# step 11: 配置用户的文件访问权限

# 节点1/2/3
# 授予用户访问文件夹和文件的权限
V_NODE_NUM=3
for ((i=1;i<=${V_NODE_NUM};i++))
do

# apollo-portal
APOLLO_PORTAL_HOME_i=/app/apollo/apollo-portal-${i}
APOLLO_PORTAL_DATA_i=/data/apollo/apollo-portal-${i}
APOLLO_PORTAL_LOG_i=/log/apollo/apollo-portal-${i}
# apollo-adminservice
APOLLO_ADMINSERVICE_HOME_i=/app/apollo/apollo-adminservice-${i}
APOLLO_ADMINSERVICE_DATA_i=/data/apollo/apollo-adminservice-${i}
APOLLO_ADMINSERVICE_LOG_i=/log/apollo/apollo-adminservice-${i}
# apollo-configservice
APOLLO_CONFIGSERVICE_HOME_i=/app/apollo/apollo-configservice-${i}
APOLLO_CONFIGSERVICE_DATA_i=/data/apollo/apollo-configservice-${i}
APOLLO_CONFIGSERVICE_LOG_i=/log/apollo/apollo-configservice-${i}

# apollo-portal
chmod -R 775 ${APOLLO_PORTAL_HOME_i} && \
chown -R ops_app_user:ops_install_group ${APOLLO_PORTAL_HOME_i} && \
chmod -R 775 ${APOLLO_PORTAL_DATA_i} && \
chown -R ops_app_user:ops_install_group ${APOLLO_PORTAL_DATA_i} && \
chmod -R 775 ${APOLLO_PORTAL_LOG_i} && \
chown -R ops_app_user:ops_install_group ${APOLLO_PORTAL_LOG_i} && \
# apollo-adminservice
chmod -R 775 ${APOLLO_ADMINSERVICE_HOME_i} && \
chown -R ops_app_user:ops_install_group ${APOLLO_ADMINSERVICE_HOME_i} && \
chmod -R 775 ${APOLLO_ADMINSERVICE_DATA_i} && \
chown -R ops_app_user:ops_install_group ${APOLLO_ADMINSERVICE_DATA_i} && \
chmod -R 775 ${APOLLO_ADMINSERVICE_LOG_i} && \
chown -R ops_app_user:ops_install_group ${APOLLO_ADMINSERVICE_LOG_i} && \
# apollo-configservice
chmod -R 775 ${APOLLO_CONFIGSERVICE_HOME_i} && \
chown -R ops_app_user:ops_install_group ${APOLLO_CONFIGSERVICE_HOME_i} && \
chmod -R 775 ${APOLLO_CONFIGSERVICE_DATA_i} && \
chown -R ops_app_user:ops_install_group ${APOLLO_CONFIGSERVICE_DATA_i} && \
chmod -R 775 ${APOLLO_CONFIGSERVICE_LOG_i} && \
chown -R ops_app_user:ops_install_group ${APOLLO_CONFIGSERVICE_LOG_i}

done
# for 循环 - end


# ==================================================================================================================================

# step 12: 启动apollo

# 1. 启动configservice: 节点1/2/3
/app/apollo/apollo-configservice-1/apollo-configservice-1.6.1/scripts/startup.sh &
/app/apollo/apollo-configservice-2/apollo-configservice-1.6.1/scripts/startup.sh &
/app/apollo/apollo-configservice-3/apollo-configservice-1.6.1/scripts/startup.sh &
# 停止configservice: 节点1/2/3
/app/apollo/apollo-configservice-1/apollo-configservice-1.6.1/scripts/shutdown.sh
/app/apollo/apollo-configservice-2/apollo-configservice-1.6.1/scripts/shutdown.sh
/app/apollo/apollo-configservice-3/apollo-configservice-1.6.1/scripts/shutdown.sh


# 2. 启动adminservice: 节点1/2/3
/app/apollo/apollo-adminservice-1/apollo-adminservice-1.6.1/scripts/startup.sh &
/app/apollo/apollo-adminservice-2/apollo-adminservice-1.6.1/scripts/startup.sh &
/app/apollo/apollo-adminservice-3/apollo-adminservice-1.6.1/scripts/startup.sh &
# 停止adminservice: 节点1/2/3
/app/apollo/apollo-adminservice-1/apollo-adminservice-1.6.1/scripts/shutdown.sh
/app/apollo/apollo-adminservice-2/apollo-adminservice-1.6.1/scripts/shutdown.sh
/app/apollo/apollo-adminservice-3/apollo-adminservice-1.6.1/scripts/shutdown.sh


# 3. 启动portal: 节点1/2/3
/app/apollo/apollo-portal-1/apollo-portal-1.6.1/scripts/startup.sh &
/app/apollo/apollo-portal-2/apollo-portal-1.6.1/scripts/startup.sh &
/app/apollo/apollo-portal-3/apollo-portal-1.6.1/scripts/startup.sh &
# 停止portal: 节点1/2/3
/app/apollo/apollo-portal-1/apollo-portal-1.6.1/scripts/shutdown.sh
/app/apollo/apollo-portal-2/apollo-portal-1.6.1/scripts/shutdown.sh
/app/apollo/apollo-portal-3/apollo-portal-1.6.1/scripts/shutdown.sh

# ------------------------------------------------


# 检查apollo: 节点1/2/3 
# 方式1：检查apollo是否启动

# 方法2：检查apollo是否启动
netstat -nltp | grep apollo


# --------------------------------------------------------------------------------------------------

# apollo 命令使用


portal == SERVER_PORT=${SERVER_PORT:=8070}

config == SERVER_PORT=${SERVER_PORT:=8080}

admin == SERVER_PORT=${SERVER_PORT:=8090}
   


# 打开防火墙端口: 节点1
# portal
firewall-cmd --zone=public --add-port=18071/tcp --permanent && \
firewall-cmd --zone=public --add-port=18072/tcp --permanent && \
firewall-cmd --zone=public --add-port=18073/tcp --permanent && \
# config
firewall-cmd --zone=public --add-port=18081/tcp --permanent && \
firewall-cmd --zone=public --add-port=18082/tcp --permanent && \
firewall-cmd --zone=public --add-port=18083/tcp --permanent && \
# admin
firewall-cmd --zone=public --add-port=18091/tcp --permanent && \
firewall-cmd --zone=public --add-port=18092/tcp --permanent && \
firewall-cmd --zone=public --add-port=18093/tcp --permanent && \
firewall-cmd --reload && \
firewall-cmd --zone=public --list-ports

# ====================================================================================================================================================

# step 13: 设置开机启动apollo

# 创建apollo service文件，如下

# 切换到root账户
su root

# 节点1/2/3
# 配置伪集群，复制三个节点 myid=1/2/3的service服务文件
V_NODE_NUM=3
for ((i=1;i<=${V_NODE_NUM};i++))
do

echo "${i}, begin the service register : apollo-configservice-${i}, ...."
cat > /etc/systemd/system/apollo-configservice-${i}.service <<EOF
[Unit]
Description=apollo-configservice-${i} service
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
User=ops_app_user
# Type=notify / forking
Type=forking
TimeoutSec=0
Environment="JAVA_HOME=/env/jdk/jdk-12.0.2"
ExecStart=/app/apollo/apollo-configservice-${i}/apollo-configservice-1.6.1/scripts/startup.sh
ExecStop=/app/apollo/apollo-configservice-${i}/apollo-configservice-1.6.1/scripts/shutdown.sh
RestartSec=5
LimitNOFILE=1000000

[Install]
WantedBy=multi-user.target
EOF

# register service
systemctl enable apollo-configservice-${i}
systemctl daemon-reload
# start service
systemctl start apollo-configservice-${i} &
# check service
systemctl status apollo-configservice-${i}

echo "${i}, finish the service register : apollo-configservice-${i}, ...."

# ------------------------------------------------------------

echo "${i}, begin the service register : apollo-adminservice-${i}, ...."
cat > /etc/systemd/system/apollo-adminservice-${i}.service <<EOF
[Unit]
Description=apollo-adminservice-${i} service
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
User=ops_app_user
# Type=notify / forking
Type=forking
TimeoutSec=0
Environment="JAVA_HOME=/env/jdk/jdk-12.0.2"
ExecStart=/app/apollo/apollo-adminservice-${i}/apollo-adminservice-1.6.1/scripts/startup.sh
ExecStop=/app/apollo/apollo-adminservice-${i}/apollo-adminservice-1.6.1/scripts/shutdown.sh
RestartSec=5
LimitNOFILE=1000000

[Install]
WantedBy=multi-user.target
EOF

# register service
systemctl enable apollo-adminservice-${i}
systemctl daemon-reload
# start service
systemctl start apollo-adminservice-${i} &
# check service
systemctl status apollo-adminservice-${i}

echo "${i}, finish the service register : apollo-adminservice-${i}, ...."

# ------------------------------------------------------------

echo "${i}, begin the service register : apollo-portal-${i}, ...."
cat > /etc/systemd/system/apollo-portal-${i}.service <<EOF
[Unit]
Description=apollo-portal-${i} service
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
User=ops_app_user
# Type=notify / forking
Type=forking
TimeoutSec=0
Environment="JAVA_HOME=/env/jdk/jdk-12.0.2"
ExecStart=/app/apollo/apollo-portal-${i}/apollo-portal-1.6.1/scripts/startup.sh
ExecStop=/app/apollo/apollo-portal-${i}/apollo-portal-1.6.1/scripts/shutdown.sh
RestartSec=5
LimitNOFILE=1000000

[Install]
WantedBy=multi-user.target
EOF

# register service
systemctl enable apollo-portal-${i}
systemctl daemon-reload
# start service
systemctl start apollo-portal-${i} &
# check service
systemctl status apollo-portal-${i}

echo "${i}, finish the service register : apollo-portal-${i}, ...."

done
# for 循环 - end


# ------------------------------------------------------------

# 14.检查apollo服务是否启动成功

# apollo-adminservice-1
systemctl status -l apollo-adminservice-1
# apollo-adminservice-1
systemctl status -l apollo-configservice-1
# /apollo-portal-1
systemctl status -l /apollo-portal-1

# 检查端口
netstat -nltp | grep 180
# 输出结果，如下图
# 18081/18082/18083 == 分别是伪节点1/2/3的apollo-configservice服务
# 18091/18092/18093 == 分别是伪节点1/2/3的apollo-adminservice服务
# 18071/18072/18073 == 分别是伪节点1/2/3的apollo-portal服务

# [root@CNT7XAPLOD01 ~]# netstat -nltp | grep 180
# tcp        0      0 0.0.0.0:18081           0.0.0.0:*               LISTEN      14667/java          
# tcp        0      0 0.0.0.0:18083           0.0.0.0:*               LISTEN      14662/java          
# tcp        0      0 0.0.0.0:18091           0.0.0.0:*               LISTEN      15861/java          
# tcp        0      0 0.0.0.0:18092           0.0.0.0:*               LISTEN      15904/java          
# tcp        0      0 0.0.0.0:18093           0.0.0.0:*               LISTEN      15928/java          
# tcp        0      0 0.0.0.0:18071           0.0.0.0:*               LISTEN      17070/java          
# tcp        0      0 0.0.0.0:18072           0.0.0.0:*               LISTEN      17058/java          
# tcp        0      0 0.0.0.0:18073           0.0.0.0:*               LISTEN      17078/java  

# ====================================================================================================================================================